package com.kaguya.strategy;

/**
 * 行为型：策略模式
 * 定义算法族群，让算法的变化独立于本身。
 * 例如：在执行一道程序时，可能会出现A方案，后续可能会因方案的变化，要切换到B方案去，用什么方案取决于领导
 */
public class Test {
    public static void main(String[] args) {
        Work work1 = new Work();
        work1.setPlan(new PlanA());
        work1.doWork();
        System.out.println("-------");
        Work work2 = new Work();
        work2.setPlan(new PlanB());
        work2.doWork();
    }
}

class Work {
    private Plan plan;

    public void setPlan(Plan plan) {
        this.plan = plan;
    }

    public void doWork() {
        System.out.println("preparing work,select a plan");
        plan.execPlan();
        System.out.println("work done");
    }
}

interface Plan {
    void execPlan();
}

class PlanA implements Plan {

    @Override
    public void execPlan() {
        System.out.println("Plan A");
    }
}

class PlanB implements Plan {

    @Override
    public void execPlan() {
        System.out.println("Plan B");
    }
}
